$(document).ready(function() {
	
	//start pikachoose
	$(document).ready(
			function (){
				$("#piccontainer").fadeTo("slow", 1);
				$("#pikame_compact").PikaChoose({user_thumbs:true,delay_caption:false,show_prev_next:false,thumb_width:10,thumb_height:10,buttons_text: { play: "", stop: "", previous: "", next: "" }});
		});
	//end pikachoose
	
	$("#nextImage").click(getNext);
	$("#previousImage").click(getPrevious);
	// Hack to set opacity in case we only have one fit
	if ($("numFits").text() == 1) {
		$("#nextImage").css("opacity", ".5");
		$("#nextImage").unbind();
		$("#previousImage").unbind();
	}	
});

var count = 0;

// Manages the opacity of the paging buttons and keeps count updated in DOM
function buttonManager(numFits) {
	$("currentFit").text(count+1);
	if (count == (numFits-1)) {
		$("#nextImage").css("opacity", ".5");
		$("#nextImage").unbind();
	}
	else {
		$("#nextImage").css("opacity", "1");
		$("#nextImage").bind("click", getNext);
	}
	
	if (count > 0)
	{
		$("#previousImage").css("opacity", "1");
		$("#previousImage").bind("click", getPrevious);
	}
	else {
		$("#previousImage").css("opacity", ".5");
		$("#previousImage").unbind();
	}
}

function getNext() {
	$.getJSON("/noassertions/access/fit/" + $("#riderid").text(),
			function(json) {
		// Button is still clickable, so we need to guard against action if we are at the end of our set
		if (count < (json.fits.length-1))
		{
			count++;
		}
		
		populateImage(json.fits[count].fitid);
		buttonManager(json.fits.length);
		$("dateofmeasurement").html(json.fits[count].dateofmeasurement);
		$("saddleheight").html(json.fits[count].saddleheight);
		$("centertobars").html(json.fits[count].centertobars);
		$("centertosaddle").html(json.fits[count].centertosaddle);
		$("floortosaddle").html(json.fits[count].floortosaddle);
		$("floortobars").html(json.fits[count].floortobars);
		$("notes").html(json.fits[count].notes);	
	});
}

function populateImage(fitid) {
	$.getJSON("/noassertions/access/image/" + fitid,
			function(json) {
		$(".pika_main").remove();
		// This is terrible. There has to be a better way to do this
		var tempV = "";
		$.each(json.images, function(i,image) {tempV = tempV + " <li><img ref=\"/noassertions/img/" + image.filename + "\" src=\"/noassertions/img/thumb-circle-active.png\" /> <span>" + image.label + "</span></li>"});
		$("#pikame_compact").html(tempV);
		$("#pikame_compact").PikaChoose({user_thumbs:true, show_prev_next:false,thumb_width:10,thumb_height:10,buttons_text: { play: "", stop: "", previous: "", next: "" }});
		

	});
	
}

function getPrevious() {
	// Button is still clickable, so we need to guard against action if we are at the end of our set
	if (count > 0)
	{
	count--;
	}
	$.getJSON("/noassertions/access/fit/" + $("#riderid").text(),
			function(json) {
		buttonManager(json.fits.length);
		$("dateofmeasurement").html(json.fits[count].dateofmeasurement);
		$("saddleheight").html(json.fits[count].saddleheight);
		$("centertobars").html(json.fits[count].centertobars);
		$("centertosaddle").html(json.fits[count].centertosaddle);
		$("floortosaddle").html(json.fits[count].floortosaddle);
		$("floortobars").html(json.fits[count].floortobars);
		$("notes").html(json.fits[count].notes);
		populateImage(json.fits[count].fitid);	
	});
}